package eraikikon;

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import java.awt.Dimension;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Vector;
import java.awt.Point;
/**
 * BEZEROA<BR>
 * Irakasle gatazkatsuena lortu eta gero intzidentzia guztiak erakusten ditu.
 * @author SI3taldea
 *
 */
public class EI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private Kudeatzailea k = new Kudeatzailea();
	private ResultSetTableModelD tableModel;
	public static final String zerbitzuIzena = "sarreraSisteman";
	private EI_IG2 ei2=new EI_IG2(this);

	/**
	 * This is the default constructor
	 */
	public EI() {
		super();
		initialize();
		Box box=Box.createHorizontalBox();
		Box box1=Box.createVerticalBox();
		JLabel jLabel1=new JLabel("Sar ezazu hilabete kopurua:");
		JTable jTable1=new JTable();
		JScrollPane jScrollPane1= new JScrollPane(jTable1,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		final JTextField jTextField1= new JTextField();
		jTextField1.setInputVerifier(new KonprobatuOsokoa());
		JButton jButton1= new JButton("Gatazkatsuenak erakutsi");
		box1.add(jLabel1);
		box1.add(jTextField1);
		box.add(box1);
		box.add(jButton1);
		try {
			tableModel=new ResultSetTableModelD("com.mysql.jdbc.Driver","jdbc:mysql://localhost/eraikikon2","ikasle01","ikasle01","SELECT * FROM erabiltzailea");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		JTable resultTable= new JTable(tableModel);
		add(box,BorderLayout.NORTH);
		add(new JScrollPane(resultTable),BorderLayout.CENTER);
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				Vector<Integer> ik=k.getIrakasleak();
				Vector<IrakasleUkatua> iul= new Vector<IrakasleUkatua>();
				Vector<SarbideEskaera> se = new Vector<SarbideEskaera>();
				IrakasleUkatua iu;
				String s=jTextField1.getText();
				Integer z=(Integer)Integer.parseInt(s);
				for (int i=0;i<ik.size();i++){
				   Integer tx=k.getTxartel2Urte(ik.elementAt(i));
					se= k.getSarbideEskaeraUkatuak(tx,z);
					if(!se.isEmpty()){
					
						iu=new IrakasleUkatua(se.elementAt(0).getErabiltzaileID(),se.size(),se.elementAt(0).getTxartelIrakurgailuId(),tx);
						iul.addElement(iu);
					}
				}

				Collections.sort(iul);
				Collections.reverse(iul);
				ei2.idatziIrakasleak(iul);
				ei2.bidaliTableModel(tableModel);
				ei2.setVisible(true);
				 }
			
		});
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(531, 247);
		this.setLocation(new Point(400, 200));
		this.setContentPane(getJContentPane());
		this.setTitle("Gatazkatsuenen bilaketa");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}
	public static void main(String [] args){
		  EI proba = new EI();
		  proba.setVisible(true);
		  proba.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  
	  }
}  //  @jve:decl-index=0:visual-constraint="10,10"
